/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GastoSegunObjetoListar.java
 *
 * Created on 07/03/2011, 04:21:53 PM
 */
package org.gingresos.regimenimpositivo;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import org.gingresosapi.beans.*;
import org.gingresosapi.controller.*;
import org.gingresosapi.generales.Mensajeria;
import org.gingresos.start.MySesion;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author avbravo
 */
public class SubgrupoRegimenListar extends javax.swing.JInternalFrame {

    DefaultTableModel modelo;
    int fila = -1;
    GruporegimenController gruporegimenController1;
    Gruporegimen gruporegimen1;
    SubgruporegimenController subgruporegimenController1;
    Subgruporegimen subgruporegimen1;
    
    String nombreGrupoRegimen="";

    /** Creates new form GastoSegunObjetoListar */
    public SubgrupoRegimenListar() {
        initComponents();
        gruporegimenController1 = new GruporegimenController();
        subgruporegimenController1 = new SubgruporegimenController();
      gruporegimen1 = new  Gruporegimen();
        subgruporegimen1 = new Subgruporegimen();
       
        modelo = (DefaultTableModel) jXTable1.getModel();
        if (gruporegimenController1.selectAll()) {
            for (Gruporegimen gruporegimen : gruporegimenController1.getGruporegimenlist()) {
                jXComboBoxGrupo.addItem(gruporegimen.getIdgruporegimen() + " - " + gruporegimen.getGrupo());
            }
        }

        AutoCompleteDecorator.decorate(this.jXComboBoxGrupo);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jXComboBoxGrupo = new org.jdesktop.swingx.JXComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jButtonImprimir = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jButtonImprimirTodos = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Gastos Segun Objeto Listar");

        jLabel2.setText("Grupo");

        jXComboBoxGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXComboBoxGrupoActionPerformed(evt);
            }
        });

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "SubGrupo", "Grupo"
            }
        ));
        jScrollPane1.setViewportView(jXTable1);

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/print.png"))); // NOI18N
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/return.png"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jButtonImprimirTodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/print.png"))); // NOI18N
        jButtonImprimirTodos.setText("Todos");
        jButtonImprimirTodos.setToolTipText("Imprimir todos");
        jButtonImprimirTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jXComboBoxGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 674, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(318, 318, 318)
                .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonImprimirTodos, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonCerrar, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                .addGap(107, 107, 107))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jXComboBoxGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonCerrar, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                    .addComponent(jButtonImprimirTodos, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                    .addComponent(jButtonImprimir, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE))
                .addGap(40, 40, 40))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        getAccessibleContext().setAccessibleName("Sub Grupo Regimen Listar");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jXComboBoxGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXComboBoxGrupoActionPerformed
        // TODO add your handling code here:
        try {
            int j = jXComboBoxGrupo.getSelectedIndex();
            if (j != -1) {

////Remove all TableData
                //limpia el jtable
                if (modelo != null) {
                    while (modelo != null && modelo.getRowCount() > 0) {
                        modelo.removeRow(0);
                    }
                }


                gruporegimen1 = gruporegimenController1.getGruporegimenlist().get(j);
this.nombreGrupoRegimen = gruporegimen1.getIdgruporegimen() +"-"+gruporegimen1.getGrupo();
                Map<String, String> parametros = new HashMap<String, String>();
                parametros.put("idgruporegimen", gruporegimen1.getIdgruporegimen());

                boolean encontrado = subgruporegimenController1.locateByParameters(parametros);


                if (!encontrado) {
                    // Mensajeria.Mensaje(" No existen registros para este grupo");
                } else {
                    Object[] filas = new Object[3]; // Hay tres columnas en la tabla
                    for (Subgruporegimen subgruporegimen:subgruporegimenController1.getSubgruporegimenlist()) {
                       

                        filas[0] = subgruporegimen.getIdsubgruporegimen();
                        filas[1] = subgruporegimen.getSubgrupo();

                        filas[2] = subgruporegimen.getIdgruporegimen();
                        modelo.addRow(filas);

                    }
                }
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "Filtrar()");
        }
    }//GEN-LAST:event_jXComboBoxGrupoActionPerformed

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed
// TODO add your handling code here:
    try {

        HashMap map = new HashMap();
        map.put("p_municipio", MySesion.getMunicipio());
        map.put("p_gruporegimen",this.nombreGrupoRegimen);
      
            Imprimir(subgruporegimenController1.getSubgruporegimenlist(), map,false);
       


    } catch (Exception ex) {
        Mensajeria.MensajeError(ex, "Imprimir()");
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
// TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonImprimirTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirTodosActionPerformed
      try {

        HashMap map = new HashMap();
        map.put("p_municipio", MySesion.getMunicipio());
     
      if (subgruporegimenController1.selectAll()) {
            Imprimir(subgruporegimenController1.getSubgruporegimenlist(), map,true);
      }


    } catch (Exception ex) {
        Mensajeria.MensajeError(ex, "Imprimir()");
    }
    }//GEN-LAST:event_jButtonImprimirTodosActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new GastoSegunObjetoListar().setVisible(true);
//            }
//        });
//    }

    private void Imprimir(List<Subgruporegimen> aux, HashMap map,boolean todos) throws JRException {
        try {

            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(aux);
                   InputStream reporte;
           if(todos){
                reporte = (InputStream) getClass().getResourceAsStream("/org/gema/reportes/regimenimpositivo/SubgruporegimenTodos.jasper");
           }else{
               reporte = (InputStream) getClass().getResourceAsStream("/org/gema/reportes/regimenimpositivo/Subgruporegimen.jasper"); 
           }
            
            if (reporte == null) {
                Mensajeria.Mensaje("No se pudo cargar el reporte");
            } else {

                JasperPrint print = JasperFillManager.fillReport(reporte, map, ds);
                JasperViewer.viewReport(print, false);

            }

        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "Imprimir()");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonImprimirTodos;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXComboBox jXComboBoxGrupo;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
